﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevCore.Utility;

namespace DevCore.Web.UIPage
{
    /// <summary>
    /// WebForm帮助辅助类
    /// </summary>
    public class WebFormHelper
    {

        #region 界面控件与实体互相转换

        /// <summary>
        /// 根据实体的名称，将控件中对应的名称的控件的值，赋值到实体中。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="control"></param>
        public static void SetControlValueToEntity<T>(T entity, Control control)
        {
            String rValue = String.Empty;
            Type type = entity.GetType();
            PropertyInfo[] arrProperties = type.GetProperties();
            foreach (PropertyInfo propertyInfo in arrProperties)
            {
                rValue = GetControlValue(control, "c_" + propertyInfo.Name);
                if (!String.IsNullOrEmpty(rValue))
                {
                    propertyInfo.SetValue(entity, Convert.ChangeType(rValue, propertyInfo.PropertyType), null);
                }

            }
        }

        /// <summary>
        /// 得到具体控件的值
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="controlId"></param>
        /// <returns></returns>
        private static String GetControlValue(Control ctrl, String controlId)
        {
            String rValue = String.Empty;
            Control curControl = GetControlById(ctrl, controlId);
            if (curControl != null)
            {
                if (curControl is Label)
                {
                    Label lb = (Label)curControl;
                    rValue = lb.Text;
                }

                // 文本框
                if (curControl is TextBox)
                {
                    TextBox edt = (TextBox)curControl;
                    rValue = edt.Text;
                }

                //单个CheckBox
                if (curControl is CheckBox)
                {
                    CheckBox cb = (CheckBox)curControl;
                    if (cb.Checked)
                    {
                        rValue = "1";
                    }
                    else
                    {
                        rValue = "0";
                    }
                }

                ///CheckBox组
                if (curControl is CheckBoxList)
                {
                    CheckBoxList cbl = (CheckBoxList)curControl;
                    for (int i = 0; i < cbl.Items.Count; i++)
                    {
                        ListItem curItem = cbl.Items[i];
                        if (i == cbl.Items.Count - 1)
                        {
                            if (curItem.Selected)
                            {
                                rValue = rValue + curItem.Value;
                            }
                            else
                            {
                                rValue = rValue + "";
                            }
                        }
                        else
                        {
                            if (curItem.Selected)
                            {
                                rValue = rValue + curItem.Value + ",";
                            }
                            else
                            {
                                rValue = rValue + ",";
                            }
                        }
                    }

                }

                ///下拉框
                if (curControl is DropDownList)
                {
                    DropDownList ddl = (DropDownList)curControl;
                    rValue = ddl.SelectedValue;
                }

                //Raidobutton
                if (curControl is RadioButton)
                {
                    RadioButton rb = (RadioButton)curControl;
                    if (rb.Checked)
                    {
                        rValue = rValue + "1";
                    }
                    else
                    {
                        rValue = rValue + "0";
                    }
                }

                if (curControl is RadioButtonList)
                {
                    RadioButtonList rbl = (RadioButtonList)curControl;
                    rValue = rbl.SelectedValue;
                }

                if (curControl is HiddenField)
                {
                    HiddenField hiddenField = (HiddenField)curControl;
                    rValue = hiddenField.Value;
                }



            }
            return rValue;
        }

        /// <summary>
        /// 根据控件的Id,得到控件
        /// </summary>
        /// <param name="curControl">父控件</param>
        /// <param name="controlId">要查找的控件Id</param>
        /// <returns>返回找到的控件</returns>
        private static Control GetControlById(Control curControl, String controlId)
        {
            Control tmpControl = null;

            tmpControl = curControl.FindControl(controlId);
            if (tmpControl == null)
            {
                foreach (Control childControl in curControl.Controls)
                {
                    tmpControl = GetControlById(childControl, controlId);
                    if (tmpControl != null) break;
                }
            }
            return tmpControl;
        }

        #endregion 界面控件与实体互相转换

        /// <summary>
        /// 将实体的值赋值到控件中。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="curControl"></param>
        public static void SetControlValueByEntity<T>(T entity, Control curControl)
        {
            if (curControl == null) return;


            String rValue = String.Empty;
            Type type = entity.GetType();
            PropertyInfo[] arrProperties = type.GetProperties();

            foreach (PropertyInfo propertyInfo in arrProperties)
            {
                object value = type.GetProperty(propertyInfo.Name).GetValue(entity, null);
                Control objControl = GetControlById(curControl, "c_" + propertyInfo.Name);
                SetControlValue(objControl, value);
            }

        }

        /// <summary>
        /// 设置具体控件的值
        /// </summary>
        /// <param name="curControl"></param>
        /// <param name="value"></param>
        public static void SetControlValue(Control curControl, object value)
        {
            if (curControl == null) return;
            if (value == null) return;

            Type type = curControl.GetType();

            if (curControl is Label)
            {
                PropertyInfo propertyInfo = type.GetProperty("Text");
                propertyInfo.SetValue(curControl, Convert.ChangeType(value, typeof(String)), null);
            }

            ///文本框
            if (curControl is TextBox)
            {
                PropertyInfo propertyInfo = type.GetProperty("Text");
                if (propertyInfo != null)
                {
                    propertyInfo.SetValue(curControl, Convert.ChangeType(value.ToString().Trim(), typeof(String)), null);

                }

            }
            ///隐藏域
            if (curControl is HiddenField)
            {
                PropertyInfo propertyInfo = type.GetProperty("Value");
                propertyInfo.SetValue(curControl, Convert.ChangeType(value.ToString().Trim(), typeof(String)), null);

            }

            ///选择框
            if (curControl is CheckBox)
            {
                PropertyInfo propertyInfo = type.GetProperty("Checked");
                propertyInfo.SetValue(curControl, Convert.ChangeType(value, typeof(Boolean)), null);
            }

            //CheckBox组设置
            if (curControl is CheckBoxList)
            {
                CheckBoxList cbl = (CheckBoxList)curControl;
                ListItemCollection itemCollection = cbl.Items;
                String itemValue = value.ToString();
                String[] arrItemValue = itemValue.Split(',');


                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    ListItem curItem = cbl.Items[i];
                    // 判断当前的数组是否存在
                    if (i <= arrItemValue.Length)
                    {
                        if (curItem.Value.ToLower().Equals(arrItemValue[i].ToLower()))
                        {
                            curItem.Selected = true;
                        }
                        else
                        {
                            curItem.Selected = false;
                        }
                    }

                }


            }

            if (curControl is RadioButtonList)
            {
                String itemValue = value as String;
                if (itemValue == null) return;


                RadioButtonList rbl = (RadioButtonList)curControl;
                rbl.SelectedValue = itemValue;

            }

            ///下拉框
            if (curControl is DropDownList)
            {
                PropertyInfo propertyInfo = type.GetProperty("SelectedValue");
                propertyInfo.SetValue(curControl, Convert.ChangeType(value, typeof(String)), null);
            }




        }

        /// <summary>
        /// 将数据绑定到dropdownList控件中
        /// </summary>
        /// <param name="ddl"></param>
        /// <param name="dtInfo"></param>
        /// <param name="textField"></param>
        /// <param name="valueField"></param>
        /// <param name="defaultValue"></param>
        public static void SetDropdownList(DropDownList ddl, DataTable dtInfo, String textField, string valueField, String defaultValue)
        {
            if (dtInfo == null) return;
            if (String.IsNullOrEmpty(defaultValue)) return;


            ddl.DataTextField = textField;
            ddl.DataValueField = valueField;
            ddl.DataSource = dtInfo;
            ddl.DataBind();

            if (ddl.Items.FindByValue(defaultValue) != null)
            {
                ddl.SelectedValue = defaultValue;

            }
        }
    }
}
